<?php
include 'int.php';

if (isset($_POST["session"]) && $_POST["session"]!=""){	
	if ($stmt = $mysqlpdo->prepare("SELECT g.u_num, g.user FROM join_game g WHERE g.session=?")) {
		$stmt->bindParam(1, $_POST["session"], PDO::PARAM_STR);
		$stmt->execute();
		if ($stmt->rowCount()==0){
			exit();
		}
		$current_user = $stmt->fetch(PDO::FETCH_BOTH);
	}else{
		print("<div style=\"errmsg\">Error in SQL1</div>");
		exit();
	}
	
	if ($stmt4 = $mysqlpdo->prepare("SELECT * FROM join_game WHERE joined>0")){
		$stmt4->execute();
		if ($stmt5 = $mysqlpdo->prepare("INSERT INTO players (player, name, session, pic) VALUES (?, ?, ?, ?)")){
			while ($result = $stmt4->fetch(PDO::FETCH_BOTH)){
				$stmt5->bindParam(1, $result[0]);
				$stmt5->bindParam(2, $result[2], PDO::PARAM_STR);
				$stmt5->bindParam(3, $result[4], PDO::PARAM_STR);
				switch ($result[5]){
					case "1":
						$image_path = "tywong.jpg";							
						break;
					case "2":
						$image_path = "head.jpg";	
						break;
					case "3":
						$image_path = "chanfangcheung.jpg";
						break;
					case "4":
						$image_path = "tsuilapchee.jpg";
						break;
					case "5":
						$image_path = "cc.gif";
						break;
					case "6":
						$image_path = "na.jpg";
						break;
					case "7":
						$image_path = "uc.gif";
						break;
					case "8":
						$image_path = "shaw.gif";
						break;
					case "9":
						$image_path = "cuhk.jpg";
						break;
					default:
						$image_path = "tywong.jpg";	
						break;
				}
				$stmt5->bindParam(4, $image_path, PDO::PARAM_STR);	
				$stmt5->execute();
			}
		}
	}
	
	if ($stmt2 = $mysqlpdo->prepare("UPDATE system_flag SET flag=1 WHERE name='game_start'")){
		$stmt2->execute();
	}else{
		print("<div style=\"errmsg\">Error in SQL2</div>");
		exit();
	}
}

?>